package com.openlab.callsystem.service.impl;


import com.openlab.callsystem.controller.OneWebSocket;
import com.openlab.callsystem.entity.Log;
import com.openlab.callsystem.enums.BusinessType;
import com.openlab.callsystem.enums.WindowStatus;
import com.openlab.callsystem.mapper.LogMapper;
import com.openlab.callsystem.service.LineUpService;
import com.openlab.callsystem.utils.QueueBeans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.websocket.Session;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicInteger;
@Service
public class LineUp implements LineUpService {
    @Autowired
    QueueBeans queueBeans;
    //办理完成调用
    @Autowired
    LogMapper logMapper;
    @Override
    public void finishBussiness(Log log){
        /*
        mark:普通 or Vip
        takeIP：办理机器号
        Number：取号时的号码
         */
        CopyOnWriteArraySet set;
        ConcurrentHashMap window;
        System.out.println(log.getBusinessType());
        if (log.getBusinessType().equals(BusinessType.Common.getValue())){
            set= queueBeans.getCommonWork();
            window=queueBeans.getCommonWindows();
            int i = queueBeans.getCommonCurrent().decrementAndGet();
            System.out.println(i);
        }else {
            set=queueBeans.getVipWork();
            window=queueBeans.getVipWindows();
            queueBeans.getVipCurrent().decrementAndGet();
        }
        set.remove(log.getNum());
        window.put(log.getNum(), WindowStatus.FREE);
        //日志添加
        System.out.println(log);
        logMapper.insert(log);
    }

    @Override
    public Integer CallVipNumber(Integer windowsId){
        int number;
        ConcurrentLinkedQueue<Integer> queue = queueBeans.getVipQueues();
        synchronized (queue){
            number=queue.poll();
            queueBeans.getVipWork().add(number);
        }
        Session session= OneWebSocket.map.get("session");
        try {
            session.getBasicRemote().sendText(String.valueOf(number)+"."+windowsId);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return number;
    }
    @Override
    public Integer CallCommonNumber(Integer windowsId){
        int number;
        ConcurrentLinkedQueue<Integer> queue = queueBeans.getCommonQueues();
        synchronized (queue){
            number=queue.poll();
            queueBeans.getCommonWork().add(number);
        }
        Session session = OneWebSocket.map.get("session");
        try {
            session.getBasicRemote().sendText(String.valueOf(number)+"."+windowsId);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return number;
    }
    //vip取号
    @Override
    public Map vipLineUp(){
        AtomicInteger vipNumber = queueBeans.getVipNumber();
        AtomicInteger vipCurrent = queueBeans.getVipCurrent();
        int number,current;
        Map<String,Integer> map=new HashMap<>();
        synchronized (vipNumber){
            number= vipNumber.incrementAndGet();
            current=vipCurrent.incrementAndGet();
            queueBeans.getVipQueues().add(number);
            map.put("number",number);
            map.put("current",current);
        }
        return map;
    }
    //普通取号
    @Override
    public Map commonLineUp(){
        AtomicInteger commonNumber = queueBeans.getCommonNumber();
        AtomicInteger commonCurrent = queueBeans.getCommonCurrent();
        int number,current;
        Map<String,Integer> map=new HashMap<>();
        synchronized (commonNumber){
             number= commonNumber.incrementAndGet();
             current=commonCurrent.incrementAndGet();
             queueBeans.getCommonQueues().add(number);
             map.put("number",number);
             map.put("current",current);
        }
        return map;
    }
}